/*
 * @file recognize_page
 * @Author: liuer
 * @Date: 2024/5/24 15:54
 * @LastModified: 2024-05-24 15:54
 * @LastModifiedBy: liuer
 */
import 'dart:async';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_scalable_ocr/flutter_scalable_ocr.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:native_dart_package/loading/loading_util.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/styles/styles.dart';
class RecognizePage extends StatefulWidget {
  const RecognizePage({super.key, required this.title});

  final String title;


  @override
  State<RecognizePage> createState() => _RecognizePageState();
}

class _RecognizePageState extends State<RecognizePage> {
  String text = "";
  final StreamController<String> controller = StreamController<String>();
  String _clipboardContent = '';
  void setText(value) {
    controller.add(value);
  }

  @override
  void dispose() {
    controller.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              ScalableOCR(
                  paintboxCustom: Paint()
                    ..style = PaintingStyle.stroke
                    ..strokeWidth = 4.0
                    ..color = const Color.fromARGB(153, 102, 160, 241),
                  boxLeftOff: 5,
                  boxBottomOff: 2.5,
                  boxRightOff: 5,
                  boxTopOff: 2.5,
                  boxHeight: ScreenUtil().screenHeight * 0.3,
                  getRawData: (value) {
                    inspect(value);
                  },
                  getScannedText: (value) {
                    setText(value);
                  }),
              StreamBuilder<String>(
                stream: controller.stream,
                builder:
                    (BuildContext context, AsyncSnapshot<String> snapshot) {
                      text = snapshot.data ?? "";
                  return Result(
                      text: snapshot.data != null ? snapshot.data! : "");
                },
              ),
              Row(
                children: [
                  Spacer(),
                  GestureDetector(
                    onTap: (){
                      _copyToClipboard(text);
                    },
                    child: Container(
                      alignment: Alignment.center,
                      height: 30,
                      width: 80,
                      decoration: BoxDecoration(
                        color: Colour.primaryColor,
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: Text("Copy",style: ytTextStyle(fontWeight: FontWeight.w500,fontSize: 20,color: Colour.white),),
                    ),
                  ),
                  Spacer()
                ],
              )
            ],
          ),
        ));
  }
  // 复制文本到剪贴板
  Future<void> _copyToClipboard(String text) async {
    await Clipboard.setData(ClipboardData(text: text));
    setState(() {
      showSuccessToast('复制成功');
      _clipboardContent = text;
    });
  }
}
class Result extends StatelessWidget {
  const Result({
    Key? key,
    required this.text,
  }) : super(key: key);

  final String text;

  @override
  Widget build(BuildContext context) {
    return Text(" $text",style: ytTextStyle(fontWeight: FontWeight.w500,fontSize: 16,));
  }
}